import {createRouter,createWebHashHistory} from "vue-router";

import Login from '../page/login.vue'
import Student from "../page/Student.vue";
import Teacher from "../page/Teacher.vue";
import Register from "../page/Register.vue";
import {ElNotification} from "element-plus";

import stuCourse from "../components/student/StuCourse.vue";
import stuCenter from "../components/student/StuCenter.vue";
import stuMain from "../components/student/StuMain.vue";
import ansQuestion from "../components/student/AnsQuestion.vue";
import courseDetial from "../components/student/CourseDetial.vue";
import ChapterContent from "../components/student/ChapterContent.vue";

import studentManage from "../components/teacher/StudentManage.vue";
import teaAnswering from "../components/teacher/TeaAnswering.vue";
import teaMain from "../components/teacher/TeaMain.vue";
import teaCourse from "../components/teacher/TeaCourse.vue";
import learningMonitoring from "../components/teacher/LearningMonitoring.vue";
import stuArticle from "../components/student/StuArticle.vue";
import classesManager from "../components/teacher/ClassesManager.vue";

import TeaQuestion from "../components/student/TeaQuestion.vue";
import TeaQuestionAns from "../components/teacher/TeaQuestionAns.vue";
import Article from "../components/student/Article.vue";
import CourseEdit from "../components/teacher/TeaCourseManage/CourseEdit.vue";
const router=createRouter({
    history:createWebHashHistory(),
    routes:[
        { path:'/',redirect:'/login'},
        { path: '/login', component: Login,name:"login" },
        { path:'/register',component:Register,name:"register"},
        { path: '/teacher', component: Teacher ,
            beforeEnter(to, from, next){
                next();
            },
            name:"teacher",
            children:[//配置二级路由  //注二级或以下的路由pat不要加/\
                { path: '', component: teaMain },
                { path: 'teaMain', component: teaMain,name:"teaMain" },
                { path: 'studentManage', component: studentManage,name:"studentManage" },
                // { path: 'teaAnswering', component: teaAnswering,name:"teaAnswering" },
                { path: 'teaAnswering', component: teaAnswering,
                    beforeEnter(to, from, next){
                        next();
                    },name:"teaAnswering",
                    children:[
                        { path: 'TeaQuestionAns', component: TeaQuestionAns,name:"TeaQuestionAns" },
                    ]

                },

                { path: 'teaCourse', component: teaCourse,name:"teaCourse" },
                { path: 'classesManager', component: classesManager,name:"classesManager" },
                { path: 'CourseEdit', component: CourseEdit,name:"CourseEdit" },
                { path: 'learningMonitoring', component: learningMonitoring,name:"learningMonitoring" },

            ]
        },
        { path: '/student', component: Student,
            beforeEnter(to, from, next){
                next();
            },
            name:"student",
            children:[//配置二级路由  //注二级或以下的路由pat不要加/\
                { path: '', component: stuMain},

                { path: 'stuMain', component: stuMain,name:"stuMain" },
                { path: 'stuCourse', component: stuCourse,name:"stuCourse" },
                { path: 'stuCenter', component: stuCenter,name:"stuCenter" },
                { path: 'stuArticle', component: stuArticle,name:"stuArticle" },
                { path: 'Article', component: Article,name:"Article" },
                { path: 'ansQuestion', component: ansQuestion,
                    beforeEnter(to, from, next){
                        next();
                    },name:"ansQuestion",
                    children:[
                        { path: 'TeaQuestion', component: TeaQuestion,name:"TeaQuestion" },
                    ]

                     },
                { path: 'courseDetial', component: courseDetial,
                    beforeEnter(to, from, next){
                        next();
                    },
                    name:"courseDetial",
                children:[
                    { path: 'ChapterContent', component: ChapterContent,name:"ChapterContent" },
                ]
                },



            ]
        }
    ]
})

router.beforeEach(function(to,from,next){ //前置路由守卫
    // console.log("跳转的目标页面为:",to);
    // console.log("从哪个页面跳转过来的:",from);

    //假设访问centerMain或centerMain下的二级部门页面时，都 要先判断用户是否登录过，没登录过则跳转到登录页面中去
    if(to.fullPath.startsWith("/student") || to.fullPath.startsWith("/teacher")){
        //是否放行取决 于是否已登录?
        let loginUserId =   window.sessionStorage.getItem("loginUserInfo");
        //没有登录过就跳转到登录界面
        if (loginUserId === null){
            ElNotification({
                title: 'Warning',
                message: "请先登录",
                type: 'warning',
            });
            router.replace("/login");
        }else {
            next()
        }
    }else {
        next()
    }
});
// 导出路由对象
export default router
